package com.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

//home button click 
class HomeActionListener extends JPanel implements ActionListener {
	MainWindows main;

	public HomeActionListener(MainWindows mainWindows) {
		main = mainWindows;
		String information = "환영합니다.";
		JLabel textLabel = new JLabel(information);
		main.homePanel.add(textLabel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		main.homePanel.setVisible(true);
		main.registerPanel.setVisible(false);
		main.loginPanel.setVisible(false);
		main.logoutPanel.setVisible(false);
		main.informationPanel.setVisible(false);
		main.exitPanel.setVisible(false);

		main.mainFrame.validate();
	}
}

// regisger button click
class RegisterActionListener extends JPanel implements ActionListener {
	MainWindows main;
	PCLoginService loginSvc;
	

	public RegisterActionListener(MainWindows mainWindows,
			PCLoginService loginService) {
		main = mainWindows;
		loginSvc = loginService;

		String information = "위와 같이 NFC장비에 휴대폰을 갖다 대 주세요";
		Icon icon = new ImageIcon("image3.gif");
		JLabel imageLabel = new JLabel(icon);
		JLabel textLabel = new JLabel(information);
		JButton cancelBtn = new JButton("Cancel");
		main.registerPanel.add(imageLabel);
		main.registerPanel.add(textLabel);
		main.registerPanel.add(cancelBtn);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		main.homePanel.setVisible(false);
		main.registerPanel.setVisible(true);
		main.loginPanel.setVisible(false);
		main.logoutPanel.setVisible(false);
		main.informationPanel.setVisible(false);
		main.exitPanel.setVisible(false);
		main.mainFrame.invalidate();
		main.mainFrame.validate();
		
	//	loginSvc.registerUser();
		
		loginSvc.runType = 1;
		Thread registerTrd = new Thread(loginSvc);
		registerTrd.start();
	}
}

// login button click
class LoginActionListener extends JPanel implements ActionListener {
	MainWindows main;
	PCLoginService loginSvc;

	public LoginActionListener(MainWindows mainWindows,
			PCLoginService loginService) {
		main = mainWindows;
		loginSvc = loginService;
		
		

		String information = "위와 같이 NFC장비에 휴대폰을 갖다 대 주세요";
		Icon icon = new ImageIcon("image3.gif");
		
		JLabel imageLabel = new JLabel(icon);
		JLabel textLabel = new JLabel(information);
		imageLabel.setSize(50, 50);
		main.loginPanel.add(imageLabel);
		main.loginPanel.add(textLabel);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		main.homePanel.setVisible(false);
		main.registerPanel.setVisible(false);
		main.loginPanel.setVisible(true);
		main.logoutPanel.setVisible(false);
		main.informationPanel.setVisible(false);
		main.exitPanel.setVisible(false);

		main.mainFrame.validate();
		loginSvc.runType = 0;
		Thread loginTrd = new Thread(loginSvc);
		loginTrd.start();

	}
}

// logout button click
class LogoutActionListener extends JPanel implements ActionListener {
	MainWindows main;
	PCLoginService loginSvc;

	public LogoutActionListener(MainWindows mainWindows,
			PCLoginService loginService) {
		main = mainWindows;
		loginSvc = loginService;

		String information = "로그아웃 되셨습니다.";
		JLabel textLabel = new JLabel(information);
		main.logoutPanel.add(textLabel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		main.homePanel.setVisible(false);
		main.registerPanel.setVisible(false);
		main.loginPanel.setVisible(false);
		main.logoutPanel.setVisible(true);
		main.informationPanel.setVisible(false);
		main.exitPanel.setVisible(false);

		main.mainFrame.validate();
		
		loginSvc.runType = 2;
		Thread logoutTrd = new Thread(loginSvc);
		logoutTrd.start();
	}
}

// information button click
class InformationActionListener extends JPanel implements ActionListener {
	MainWindows main;
	
	GridLayout gridLayout;

	JLabel textLabel;
	JLabel textLabelName;
	JLabel textLabelEmail;
	JLabel textLabelCountry;
	JLabel textLabelLocation;
	JLabel textLabelOrganization;
	JLabel textLabelSerialNumber;
	JLabel textLabelSerialDate;
	JLabel textLabelNotAfter;
	
	public InformationActionListener(MainWindows mainWindows) {
		main = mainWindows;
		
		gridLayout = new GridLayout(9,0);
		
		textLabel = new JLabel("====인증서 정보입니다====");
		textLabelName = new JLabel(PCLoginService.userName);
		textLabelEmail = new JLabel(PCLoginService.userEmail);
		textLabelCountry = new JLabel(PCLoginService.userCountry);
		textLabelLocation = new JLabel(PCLoginService.userLocation);
		textLabelOrganization = new JLabel(PCLoginService.userOrganization);
		textLabelSerialNumber = new JLabel(PCLoginService.certSerialNumber);
		textLabelSerialDate = new JLabel(PCLoginService.certNotBefore);
		textLabelNotAfter = new JLabel(PCLoginService.certNotAfter);
		
		main.informationPanel.setLayout(gridLayout);
		main.informationPanel.add(textLabel);
		main.informationPanel.add(textLabelName);
		main.informationPanel.add(textLabelEmail);
		main.informationPanel.add(textLabelCountry);
		main.informationPanel.add(textLabelLocation);
		main.informationPanel.add(textLabelOrganization);
		main.informationPanel.add(textLabelSerialNumber);
		main.informationPanel.add(textLabelSerialDate);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		main.homePanel.setVisible(false);
		main.registerPanel.setVisible(false);
		main.loginPanel.setVisible(false);
		main.logoutPanel.setVisible(false);
		main.informationPanel.setVisible(true);
		main.exitPanel.setVisible(false);
		textLabelName.setText("이름 : " + PCLoginService.userName);
		textLabelEmail.setText("Email : " + PCLoginService.userEmail);
		textLabelCountry.setText("국가 : " + PCLoginService.userCountry);
		textLabelLocation.setText("지역 : " + PCLoginService.userLocation);
		textLabelOrganization.setText("조직 : " + PCLoginService.userOrganization);
		textLabelSerialNumber.setText("인증서 일련번호 : " + PCLoginService.certSerialNumber);
		textLabelSerialDate.setText("유효기간 : " + PCLoginService.certNotBefore + " ~ " + PCLoginService.certNotAfter);
		main.mainFrame.validate();
	}
}

// Exit button click
class ExitActionListener extends JPanel implements ActionListener {
	MainWindows main;
	PCLoginService loginSvc;
	public ExitActionListener(MainWindows mainWindows,
			PCLoginService loginService) {
		main = mainWindows;
		loginSvc = loginService;
		
		String information = "종료합니다.";
		JLabel textLabel = new JLabel(information);
		main.exitPanel.add(textLabel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		main.homePanel.setVisible(false);
		main.registerPanel.setVisible(false);
		main.loginPanel.setVisible(false);
		main.logoutPanel.setVisible(false);
		main.informationPanel.setVisible(false);
		main.exitPanel.setVisible(true);

		main.mainFrame.validate();
		loginSvc.logout();
		System.exit(0);
		
	}
}

class MenuPanel extends JPanel {
	PCLoginService loginService;
	public MenuPanel(MainWindows mainWindows) {
		setBackground(new Color(221, 221, 221));// 배경색 지정

		JLabel bgLabel = new JLabel("", SwingConstants.CENTER);
		bgLabel.setIcon(new ImageIcon("bg.png"));

		JButton homeBtn = new JButton(new ImageIcon("home.png"));
		JButton rgBtn = new JButton(new ImageIcon("register.png"));
		JButton loginBtn = new JButton(new ImageIcon("login.png"));
		JButton logoutBtn = new JButton(new ImageIcon("logout.png"));
		JButton infoBtn = new JButton(new ImageIcon("information.png"));
		JButton exitBtn = new JButton(new ImageIcon("exit.png"));

		homeBtn.setBorderPainted(false);
		rgBtn.setBorderPainted(false);
		loginBtn.setBorderPainted(false);
		logoutBtn.setBorderPainted(false);
		infoBtn.setBorderPainted(false);
		exitBtn.setBorderPainted(false);

		homeBtn.setContentAreaFilled(false);
		rgBtn.setContentAreaFilled(false);
		loginBtn.setContentAreaFilled(false);
		logoutBtn.setContentAreaFilled(false);
		infoBtn.setContentAreaFilled(false);
		exitBtn.setContentAreaFilled(false);

		add(bgLabel);
		bgLabel.add(homeBtn);
		bgLabel.add(rgBtn);
		bgLabel.add(loginBtn);
		bgLabel.add(logoutBtn);
		bgLabel.add(infoBtn);
		bgLabel.add(exitBtn);

		System.out.println("ReadNFC");
		loginService = new PCLoginService(0);
		
		final ActionListener homeListener = new HomeActionListener(mainWindows);
		final ActionListener registerListener = new RegisterActionListener(
				mainWindows, loginService);
		final ActionListener loginListener = new LoginActionListener(
				mainWindows, loginService);
		final ActionListener logoutListener = new LogoutActionListener(
				mainWindows, loginService);
		final ActionListener informationListener = new InformationActionListener(
				mainWindows);
		final ActionListener exitListener = new ExitActionListener(mainWindows, loginService);

		homeBtn.addActionListener(homeListener);
		rgBtn.addActionListener(registerListener);
		loginBtn.addActionListener(loginListener);
		logoutBtn.addActionListener(logoutListener);
		infoBtn.addActionListener(informationListener);
		exitBtn.addActionListener(exitListener);

		setLayout(null);

		bgLabel.setBounds(0, -10, 120, 410);
		homeBtn.setBounds(20, 40, 80, 30);
		rgBtn.setBounds(20, 100, 80, 30);
		loginBtn.setBounds(20, 160, 80, 30);
		logoutBtn.setBounds(20, 220, 80, 30);
		infoBtn.setBounds(5, 280, 110, 30);
		exitBtn.setBounds(20, 340, 80, 30);

		setBounds(1, 80, 120, 391);
	}
	public void finalize(){
		loginService.logout();
	}
}

public class MainWindows {
	public JPanel homePanel;
	public JPanel registerPanel;
	public JPanel loginPanel;
	public JPanel logoutPanel;
	public JPanel informationPanel;
	public JPanel exitPanel;
	public JFrame mainFrame;
	private static TextArea txtLog;
	
	public static void setTextLog(String str){		
		txtLog.append("\nLog : "+str);
	}
	public MainWindows() {
		mainFrame = new JFrame("Web Login System");

		homePanel = new JPanel();
		registerPanel = new JPanel();
		loginPanel = new JPanel();
		logoutPanel = new JPanel();
		informationPanel = new JPanel();
		exitPanel = new JPanel();

		JPanel context = new JPanel();
		context.setBackground(new Color(221, 221, 221));// 배경색 지정
		context.setBounds(123, 80, 370, 391);

		MenuPanel menu = new MenuPanel(this);

		JPanel backg = new JPanel();
		backg.setBackground(new Color(221, 221, 221));

		JPanel txtPanel = new JPanel();
		txtPanel.setBackground(new Color(221, 221, 221));// 배경색 지정
		txtLog = new TextArea(
				"******************* Welecome NFC Login System *******************\n",
				29, 52);
		txtLog.setBackground(Color.white);
		txtLog.setEditable(false);
		txtPanel.add(txtLog);
		txtPanel.setBounds(500, 3, 390, 465);

		JLabel titleLabel = new JLabel("", SwingConstants.CENTER);
		JLabel HlineLable = new JLabel();
		JLabel VlineLable = new JLabel();

		titleLabel.setIcon(new ImageIcon("title.png"));
		HlineLable.setBorder(new LineBorder(Color.gray));
		VlineLable.setBorder(new LineBorder(Color.gray));

		mainFrame.add(titleLabel);
		mainFrame.add(HlineLable);
		mainFrame.add(VlineLable);
		mainFrame.add(menu);

		mainFrame.add(homePanel);
		mainFrame.add(registerPanel);
		mainFrame.add(loginPanel);
		mainFrame.add(logoutPanel);
		mainFrame.add(informationPanel);
		mainFrame.add(exitPanel);

		mainFrame.add(txtPanel);
		mainFrame.add(backg);

		titleLabel.setBounds(10, 10, 470, 80);
		HlineLable.setBounds(10, 79, 475, 1);
		VlineLable.setBounds(123, 85, 1, 385);

		homePanel.setBounds(123, 80, 370, 391);
		registerPanel.setBounds(123, 80, 370, 391);
		loginPanel.setBounds(123, 80, 370, 391);
		logoutPanel.setBounds(123, 80, 370, 391);
		informationPanel.setBounds(123, 80, 370, 391);
		exitPanel.setBounds(123, 80, 370, 391);

		homePanel.setBackground(new Color(221, 221, 221));
		registerPanel.setBackground(new Color(221, 221, 221));
		loginPanel.setBackground(new Color(221, 221, 221));
		logoutPanel.setBackground(new Color(221, 221, 221));
		informationPanel.setBackground(new Color(221, 221, 221));
		exitPanel.setBackground(new Color(221, 221, 221));

		homePanel.setVisible(true);
		registerPanel.setVisible(false);
		loginPanel.setVisible(false);
		logoutPanel.setVisible(false);
		informationPanel.setVisible(false);
		exitPanel.setVisible(false);

		// 프레임 크기 선언
		mainFrame.setSize(900, 500);

		// 현재 모니터의 크기 알아내기
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

		// 프레임의 사이즈 알아내기
		Dimension f_size = mainFrame.getSize();

		// 현재 스크린의 폭을 얻어와서 / 2 하고, 프레임의 폭을 나누기2 해서 뺀다.
		// 현재 스크린의 높이를 얻어와서 같은 동작을 한다.
		// 이렇게 함으로써 모니터 화면의 정 중앙에 우리가 만든 프레임이 위치된다.
		int xpos = (int) (screen.getWidth() / 2 - f_size.getWidth() / 2);
		int ypos = (int) (screen.getHeight() / 2 - f_size.getHeight() / 2);

		// 프레임 나타날 위치 지정하는 메소드
		mainFrame.setLocation(xpos, ypos);
//		mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//
		// 프레임 크기 고정시키기
		mainFrame.setResizable(false);

		// 프레임 보여지기 - 기본값 false
		mainFrame.setVisible(true);
		// mainFrame.repaint();
	}
}
